// 这份代码专门处理逻辑层的操作
const jobDao = require('../dao/jobDao');

const jobService = {
    /*
     * 获取所有职位
     */
    async getAllJob(uid, lastJobId) {
        let result;
        try {
            let jobList = await jobDao.getAllJob(uid, lastJobId);
            result = { success: true, msg: "查询成功", data: jobList };
        } catch (error) {
            result = { success: false, msg: "查询数据库时出错，查询失败" };
        }
        return result;
    },
    async getJobDetail(uid, jobId) {
        let result;
        try {
            let jobDetail = await jobDao.getJobDetail(uid, jobId);
            if (jobDetail.length == 0) {
                result = { success: false, msg: "查询为空" };
            } else {
                result = { success: true, msg: "查询成功", job: jobDetail[0] };
            }
        } catch (error) {
            result = { success: false, msg: "查询数据库时出错，查询失败" };
        }
        return result;
    },
    /*
     * 查询是否收藏了职位
     */
    async isCollectJob(uid, jobId) {
        let result;
        try {
            let res = await jobDao.isCollectJob(uid, jobId);
            result = { success: true, msg: "查询成功", isCollect: res[0].isCollect };
        } catch (error) {
            result = { success: false, msg: "查询数据库时出错，查询失败" };
        }
        return result;
    },
    /*
     * 收藏职位
     */
    async collectJob(uid, jobId) {
        let result;
        try {
            let res = await jobDao.collectJob(uid, jobId);
            if (res.affectedRows) {
                result = { success: true, msg: "收藏成功" };
            } else {
                result = { success: false, msg: "收藏失败" };
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    },
    /*
     * 取消收藏职位
     */
    async delCollectJob(uid, jobId) {
        let result;
        try {
            let res = await jobDao.delCollectJob(uid, jobId);
            if (res.affectedRows) {
                result = { success: true, msg: "取消收藏成功" };
            } else {
                result = { success: false, msg: "取消收藏失败" };
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    },
    /*
     * 添加个人简历接口
     */
    async addResume(uid, resume) {
        let { name, educationId, position, email, phone, college, studyingTime, major, professional, projExperience, workExperience } = resume;
        let result;
        try {
            let res = await jobDao.addResume(uid, name, educationId, position, email, phone, college, studyingTime, major, professional, projExperience, workExperience);
            if (res.insertId) {
                result = { success: true, msg: "添加个人简历成功" };
            } else {
                result = { success: false, msg: "添加个人简历失败" };
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    },
    /*
     * 查询个人简历接口
     */
    async getResume(uid) {
        let result;
        try {
            let res = await jobDao.getResume(uid);
            if (res.length) {
                result = { success: true, msg: "查询个人简历成功", resume: res[0] };
            } else {
                result = { success: false, msg: "查询个人简历为空", resume: {} };
            }
        } catch (error) {
            result = { success: false, msg: "查询数据库时出错" };
        }
        return result;
    },
    /*
     * 修改个人简历接口
     */
    async updateResume(uid, resume) {
        let { name, educationId, position, email, phone, college, studyingTime, major, professional, projExperience, workExperience } = resume;
        let result;
        try {
            let res = await jobDao.addResume(uid, name, educationId, position, email, phone, college, studyingTime, major, professional, projExperience, workExperience);
            if (res.insertId) {
                result = { success: true, msg: "修改个人简历成功" };
            } else {
                result = { success: false, msg: "修改个人简历失败" };
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    },
    /*
     * 岗位投递接口
     */
    async resumeSubmission(uid, jobId) {
        let result;
        try {
            let res = await jobDao.resumeSubmission(uid, jobId);
            if (res.insertId) {
                result = { success: true, msg: "岗位投递成功" };
            } else {
                result = { success: false, msg: "岗位投递失败" };
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    },
    /*
     * 删除投递记录接口
     */
    async delResumeSubmission(uid, resumeSubmissionId) {
        let result;
        try {
            let res = await jobDao.delResumeSubmission(uid, resumeSubmissionId);
            if (res.changedRows) {
                result = { success: true, msg: "删除岗位投递记录成功" };
            } else {
                result = { success: false, msg: "删除岗位投递记录失败" };
            }
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    },
    /*
     * 获取所有投递记录接口
     */
    async getAllResumeSubmission(uid) {
        let result;
        try {
            let res = await jobDao.getAllResumeSubmission(uid);
            result = { success: true, msg: "查询所有岗位投递记录成功", data: res };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错" };
        }
        return result;
    }
};

module.exports = jobService;
